<?php 
class CartController extends Controller{
	public function __init(){
		//初始化购物车
		if(!isset($_SESSION['cart'])){
			$_SESSION['cart']=array();
		}
	}

	//购物车列表
	public function index(){
		$this->display();
	}

	public function addCart(){
		$cartId=Q('post.attr');
		$gid=Q('post.gid',0,'intval');
		if(isset($_SESSION['cart']['goods'][$cartId])){
			$_SESSION['cart']['goods'][$cartId]['num']+=$_POST['num'];//累加
			// $_SESSION['cart']['goods'][$cartId]['num']=$_POST['num'];//本次操作的数量
			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$_POST['num']*$_SESSION['cart']['goods'][$cartId]['price'];
		}else{
			//新增的商品 1)价格 2)属性
			$goods=M('goods')->find($gid);
			//查找货品的属性名
			$product=M('product')->find($_POST['product_id']);
			$attr_value=M('goods_attr')->where(" id IN(".str_replace('-',',',$product['goods_attr']).")")->getField('attr_value',true);
			$_SESSION['cart']['goods'][$cartId]['cart_id']=$cartId;
			$_SESSION['cart']['goods'][$cartId]['goods_name']=$goods['goods_name'];
			$_SESSION['cart']['goods'][$cartId]['attr_value']="[".implode(",",$attr_value)."]";
			$_SESSION['cart']['goods'][$cartId]['gid']=$_POST['gid'];
			$_SESSION['cart']['goods'][$cartId]['price']=$goods['goods_price'];
			$_SESSION['cart']['goods'][$cartId]['product_id']=$_POST['product_id'];
			$_SESSION['cart']['goods'][$cartId]['num']=$_POST['num'];
			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$_POST['num']*$goods['goods_price'];
			$_SESSION['cart']['goods'][$cartId]['pic']=$goods['goods_thumb'];
		}
		//计算商品总价
		$totalPrice=$totalNum=0;
		foreach ($_SESSION['cart']['goods'] as $g) {
			$totalPrice+=$g['num']*$g['price'];
			$totalNum+=$g['num'];
		}
		$_SESSION['cart']['totalPrice']=$totalPrice;
		$_SESSION['cart']['totalNum']=$totalNum;
		// p($_SESSION);
		// $this->display();
		$this->success('添加购物车');
		//异步弹框添加购物车

	}

	//添加购物车
	public function add(){
		// p($_POST);
		if(!Q('post.num',0,'intval') || !Q('post.gid',0,'intval') || !Q('post.product_id',0,'intval')){
			$this->error('商品不存在',__ROOT__);
		}

		$cartId=$this->getCartId();
		//判断商品是否已在购物车中存在，已经存在的只需要修改商品数量即可
		if(isset($_SESSION['cart']['goods'][$cartId])){
			$_SESSION['cart']['goods'][$cartId]['num']=$_POST['num'];
			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$_POST['num']*$_SESSION['cart']['goods'][$cartId]['price'];
		}else{
			//新增的商品 1)价格 2)属性
			$goods=M('goods')->find($_POST['gid']);
			//查找货品的属性名
			$product=M('product')->find($_POST['product_id']);
			$attr_value=M('goods_attr')->where(" id IN(".str_replace('-',',',$product['goods_attr']).")")->getField('attr_value',true);
			$_SESSION['cart']['goods'][$cartId]['cart_id']=$cartId;
			$_SESSION['cart']['goods'][$cartId]['goods_name']=$goods['goods_name'];
			$_SESSION['cart']['goods'][$cartId]['attr_value']="[".implode(",",$attr_value)."]";
			$_SESSION['cart']['goods'][$cartId]['gid']=$_POST['gid'];
			$_SESSION['cart']['goods'][$cartId]['price']=$goods['goods_price'];
			$_SESSION['cart']['goods'][$cartId]['product_id']=$_POST['product_id'];
			$_SESSION['cart']['goods'][$cartId]['num']=$_POST['num'];
			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$_POST['num']*$goods['goods_price'];
		}
		//计算商品总价
		$totalPrice=$totalNum=0;
		foreach ($_SESSION['cart']['goods'] as $g) {
			$totalPrice+=$g['num']*$g['price'];
			$totalNum+=$g['num'];
		}
		$_SESSION['cart']['totalPrice']=$totalPrice;
		$_SESSION['cart']['totalNum']=$totalNum;
		// p($_SESSION);
		$this->display();
		//调用add模版
	}

	//获取商品ID与货品ID的链接(gid-product_id)
	private function getCartId(){
		return $_POST['gid'].'-'.$_POST['product_id'];
	}

	public function changeNum(){
		$cart_id=Q('cart_id');
		$num=Q('num');
		if(!$cart_id || !$num || !isset($_SESSION['cart']['goods'][$cart_id])){
			$this->error('修改失败');
		}else{
			$_SESSION['cart']['goods'][$cart_id]['num']=$num;
			$this->success('修改成功');
		}

	}

	// public function order(){
		
	// }


}

?>